package com.cn.system.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.cn.system.common.dto.SystemGroup;
import com.cn.system.common.service.CommonService;

@Repository
@Service("commonService")
public class CommonServiceImpl extends BaseRepository implements CommonService{

	@Override
	public List queryMenu() {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM SYSTEM_MENU";
		return this.queryForList(sql);
	}

	@Override
	public Map queryUserForMap(Integer id) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM SYSTEM_USER WHERE ID =?";
		return this.queryForMap(sql, id);
	}

	@Override
	public Map findByAccounts(String account) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM SYSTEM_USER WHERE ACCOUNTS = ?";
		return this.queryForMap(sql,account);
	}

	@Override
	public Map init(Map user) {
		// TODO Auto-generated method stub
		String com_identy = user.get("COMM_ORG_IDENTY").toString();
		String logsql = "SELECT PC_INDEX_TEXT TITLE ,IMAGE FROM SYSTEM_LOGO where id=?";
		Map loge = this.queryForMap(logsql, 1);
		loge.put("HREF", "");
		String homeSql = "SELECT HOME_PAGE HREF,TITLE from SYSTEM_HOME_PAGE t WHERE ID =?";
		Map home = this.queryForMap(homeSql, 1);
		String menuSql = "select p.*"+
             " from (select t1.*"+
             "        from system_menu t1"+
             "       where "+
             "           t1.system_type = 'WH'"+
             "         and t1.comm_org_identy=?"+
             "     ) p"    +
             " start with p.id = 'root'"+
             "connect by prior p.id = p.pid  order SIBLINGS by order_";
		List<Map<String, Object>> menu = this.queryForList(menuSql, com_identy);
		List<SystemGroup> groups=new ArrayList<SystemGroup>();
		for(int i=0;i<menu.size();i++){
			Map<String, Object> map=menu.get(i);
			SystemGroup group=new SystemGroup();
			group.setTitle(map.get("NAME").toString());
			group.setIcon(map.get("ICON").toString());
			group.setHref(map.get("URL")==null?"":map.get("URL").toString());
			group.setTarget(map.get("OPEN_TYPE").toString());
			group.setId(map.get("ID").toString());
			group.setPid(map.get("PID")==null?"":map.get("PID").toString());
			groups.add(group);
		}
		SystemGroup group = this.recursiveGroup(groups,"root");
		Map init = new HashMap<>();
		init.put("homeInfo", home);
		init.put("logoInfo", loge);
		init.put("menuInfo", group.getChild());
		return init;
	}
	
	public SystemGroup recursiveGroup(List<SystemGroup> list, String id) {
		SystemGroup group=this.getGroupById(list,id);//当前功能组对象
		List<SystemGroup> childGroups = this.getGroupByPid(list,id); //子功能组对象
		//遍历子功能组
		for(SystemGroup child : childGroups){
			SystemGroup n = recursiveGroup(list,child.getId()); //递归遍历
			group.getChild().add(n);
		}
		return group;
	}
	
	public SystemGroup getGroupById(List<SystemGroup> list, String id) {
		for(int i=0;i<list.size();i++){
			SystemGroup group=list.get(i);
			if(id.equals(group.getId())){
				return group;
			}
		}
		return null;
	}

	public List<SystemGroup> getGroupByPid(List<SystemGroup> list, String id) {
		List<SystemGroup> childList=new ArrayList<SystemGroup>();
		for(int i=0;i<list.size();i++){
			SystemGroup group=list.get(i);
			if(id.equals(group.getPid())){
				childList.add(group);
			}
		}
		return childList;
	}
}
